﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MiniCrawler.Entity;

namespace MiniCrawler.DataAccess.Repositories
{
    public class CategoryLinkHelper
    {
        public static CategoryLink GetLink(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static CategoryLink GetLink(CrwlDataEntities dbContext, string url)
        {
            var link = from s in dbContext.CategoryLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static CategoryLink GetLink(CategoryLink link)
        {
            if (link != null)
            {
                return GetLink(link.Value);
            }
            else
            {
                return null;
            }
        }

        public static bool IsLinkExist(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsLinkChecked(string url)
        {
            var link = GetLink(url);
            if (link != null)
            {
                return link.IsChecked;
            }
            return false;
        }

        public static List<CategoryLink> SelectUncheckedLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static List<CategoryLink> SelectUncheckedLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static List<CategoryLink> SelectUncheckedLinks(string website, int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Website == website && s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static List<CategoryLink> SelectLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static List<CategoryLink> SelectLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Level == level) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static List<CategoryLink> SelectLinks(string webSite, int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Level == level && s.Website == webSite) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<CategoryLink>();
            }
        }

        public static bool HasUncheckedLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool HasUncheckedLinks(string hostName, int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.CategoryLinks where (s.Website == hostName && s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int CreateLink(CategoryLink link)
        {
            var db = new CrwlDataEntities();
            db.CategoryLinks.Add(link);
            return db.SaveChanges();
        }

        public static int CreateLink(string value, string website, int level, DateTime dateCreated, bool isChecked, int categoryExpressionId)
        {
            var link = new CategoryLink()
            {
                DateCreated = dateCreated,
                IsChecked = isChecked,
                Level = level,
                Value = value,
                Website = website,
                CategoryExpressionId = categoryExpressionId
            };
            return CreateLink(link);
        }

        public static int UpdateLink(CategoryLink link)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, link.Value);
            currentLink.Level = link.Level;
            currentLink.IsChecked = link.IsChecked;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public static int UpdateLink(string url)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, url);
            currentLink.IsChecked = true;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

    }
}
